# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with this
# work for additional information regarding copyright ownership.  The ASF
# licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.
require 'buildr/core'
module Dojo

  class Dojoc < Compiler::Base
    
    REQUIRES = ArtifactNamespace.for(self) do |ns|
      DOJO = '1.4.1'
      DOJO_ARTIFACT_SPEC = "dojo:dojo:zip:src:#{DOJO}"
      url = "http://download.dojotoolkit.org/release-#{DOJO}/dojo-release-#{DOJO}-src.zip"
      Buildr.download(Buildr.artifact(DOJO_ARTIFACT_SPEC)=>url)
      ns.dojo! DOJO_ARTIFACT_SPEC
    end
    
    OPTIONS = [:dojo]
      
    class << self
      def dependencies #:nodoc:
        REQUIRES.artifacts
      end

      def applies_to?(project, task) #:nodoc:
        paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) }
        paths.flatten!
        # Just select if we find .js files
        paths.any? { |path| !Dir["#{path}/**/*.js"].empty? }
      end
    end
    
    Java.classpath << dependencies
    
    specify :language => :javascript, :sources => [:dojo], :source_ext => [:js], 
            :target => 'classes', :target_ext => 'js', :packaging => :jar

    def initialize(project, options) #:nodoc:
       super
       options[:dojo] = "1.4.1" if options[:dojo].nil?
       options[:localeList] = "en" if options[:localeList].nil?
       options[:cssOptimize] = "comments" if options[:cssOptimize].nil?
       options[:mode] = "default" if options[:mode].nil?
    end
    
    def compile(sources, target, dependencies) #:nodoc:
      fp = Buildr.artifact DOJO_ARTIFACT_SPEC
      case options[:mode]
        when "default" :
          zip=Buildr::Unzip.new("#{target}/../dojo"=>fp)
          zip.extract
          dojo_root_path="#{target}/../dojo/dojo-release-#{DOJO}-src"
          util_path="#{dojo_root_path}/util";
          cp = "#{util_path}/shrinksafe/js.jar:#{util_path}/shrinksafe/shrinksafe.jar"
          FileUtils.cp_r Dir.glob("#{sources}/*"), "#{dojo_root_path}/"
          savedDir=Dir.pwd
          Dir.chdir "#{util_path}/buildscripts"
          
          #we need run shrinker in directory with dojo
          Java::Commands.java('org.mozilla.javascript.tools.shell.Main',
                  [
                    "build.js",
                    "action=release",
                    "profileFile=#{sources}/#{options[:profile]}",
                    "localeList=#{options[:localeList]}",
                    "copyTests=false",
                    "cssOptimize=options[:cssOptimize]",
                    "version=#{VERSION_NUMBER}"
                  ],
                  {:classpath => [cp]}
                  )
          Dir.chdir savedDir
          FileUtils.cp_r Dir.glob("#{dojo_root_path}/release/dojo/*"), "#{target}"
        when "sources" :
          FileUtils.cp_r Dir.glob("#{sources}/*"), "#{target}"
        when "dojo" :
          zip=Buildr::Unzip.new("#{target}/../dojo"=>fp)
          zip.extract
          dojo_root_path="#{target}/../dojo/dojo-release-#{DOJO}-src"
          FileUtils.cp_r Dir.glob("#{dojo_root_path}/*"), "#{target}"
          FileUtils.remove_dir "#{target}/util"
      end
    end
  end
end

# Groovy compiler comes first, ahead of Javac, this allows it to pick
# projects that mix Groovy and Java code by spotting Groovy code first.
Buildr::Compiler.compilers.unshift Dojo::Dojoc
